import os
import requests
root = "E:Python/爬虫/"


def download_img(url, filename):
    """
      下载图片
      :param url: 图片链接地址
      :param filename: 保存路径或者名字
      :return:
      """
    save_path = root + "img/" + filename
    if os.path.exists(save_path):
        print(save_path + "已下载")
        return

    r = requests.get(url)
    with open(save_path, 'wb') as f:
        f.write(r.content)


def download_video(url, filename):
    """
    下载视频
    :param url: 视频链接
    :param filename:  保存路径或者名字
    :return:
    """
    save_path = root + "video/" + filename

    if os.path.exists(save_path):
        print(save_path + "已下载")
        return

    r = requests.get(url)
    with open(save_path, "wb") as f:
        f.write(r.content)


def mkdir(name):
    if not os.path.exists(root + name):
        os.mkdir(root + name)


def get_dir_files(dir_path, rule=".lua"):
    """
    获取某个文件夹下的所有通一类型文件
    :param dir_path: 文件夹路径
    :param rule: 文件扩展名
    :return: 文件路径
    """
    paths = []
    for f_path, dirs, fs in os.walk(dir_path):
        for f in fs:
            file_name = os.path.join(f_path, f)
            if file_name.endswith(rule):
                paths.append(file_name)
    return paths